// pages/content/index.js
import Toast from '../../ui/toast/index'
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        pic_url:app.pic_url,
        swiperList:[app.pic_url+"pic.png",app.pic_url+"pic.png",app.pic_url+"pic.png"],
        visible:false,
        login_visible:false,
        id:0,
        content:{},
        tj_list:[],
        loading:false,
        order_form:{
            token:'',
            user_name:'',
            user_tel:'',
            content:'',
        }
    },

   

    //获取表单信息
    change_form(e){
        let value = e.detail.value;
        let type = e.currentTarget.dataset.type;
        switch(type){
            case 'user_name':
                this.data.order_form.user_name = value;
                break;
            case 'user_tel':
                this.data.order_form.user_tel = value;
                break;
            case 'content':
                this.data.order_form.content = value;
                break;        
        }
    },

    //下单
    order(){
        let token = wx.getStorageSync('token');
        if(!token){
            this.setData({
                login_visible:true,
            });
            return;
        }
        this.setData({
            visible:true,
        });
        
    },

    //登录
    login(){
        this.setData({
            loading:true,
        });
        wx.login({
          success: (res) => {
            app.http.request("index.php?r=api/index/login",{code:res.code}).then((res)=>{
                this.setData({
                    loading:false,
                });
                if(res.data.result==1){
                    wx.setStorageSync('token', res.data.data);
                    this.setData({
                        visible:true,
                        login_visible:false,
                    });
                }else{
                    Toast({
                        selector:'#toast',
                        message:res.data.data,
                        context:this,
                    });
                }
            });
          },
          fail:()=>{
              this.setData({
                  loading:false,
              });
          }
        })
    },

    kf(){
        wx.showLoading({
          title: '加载中',
        })
    },

    //保存订单
    save_order(){
        this.data.order_form.token=wx.getStorageSync('token');
        app.http.request("index.php?r=api/index/order",{OrderRecorder:this.data.order_form}).then((res)=>{
            if(res.data.result==1){
                Toast({
                    selector:"#toast",
                    context:this,
                    message:res.data.data,
                });
                this.setData({
                    visible:false,
                });
                this.init_form();
            }else{
                Toast({
                    selector:"#toast",
                    message:res.data.data,
                    context:this,
                });
                if(res.data.result==2){
                    wx.setStorageSync('token', '');
                }
            }
        });
    },

    init_form(){
        this.data.order_form.user_name="";
        this.data.order_form.user_tel="";
        this.data.order_form.content="";
    },

    onVisibleChange(e) {
        this.setData({
          visible: false,
          login_visible:false,
        });
      },
    
    //获取内容
    get_content(){
        app.http.request("index.php?r=api/index/content",{id:this.data.id}).then((res)=>{
            if(res.data.result==1){
                this.setData({
                    content:res.data.data,
                    swiperList:res.data.data.goods_pics,
                });
            }else{
                Toast({
                    selector:'#toast',
                    message:res.data.data,
                    context:this,
                });
            }
        });
    },  

    //获取随机内容
    get_rand_list(){
        app.http.request("index.php?r=api/index/newlist",{param:{num:8,rand:1}}).then((res)=>{
            if(res.data.result==1){
                this.setData({
                    tj_list:res.data.data,
                });
            }
        });
    },

    //跳转内容
    to_content(e){
        let id = e.currentTarget.dataset.id;
        wx.navigateTo({
          url: '/pages/content/index?id='+id,
        })
    },

    //跳转
  to_url(e){
    let type = e.currentTarget.dataset.type;
    let url = "/pages/index/index";
    switch(type){
      case "cate":
        url = "/pages/cate/index";
        break;
      case "home":
        break;  
      case "news":
        url="/pages/news/list/index";
        break;
      case "us":
        url="/pages/us/index";
        break;    
    }
    wx.redirectTo({
      url: url,
    })
  },


  //分享
  fx(){
    wx.showShareMenu({
        withShareTicket: true,
        menus: ['shareAppMessage', 'shareTimeline'],
        fail:(res)=>{
            console.log(res);
        }
      })
  },

  //点击
  hits(){
    app.http.request("index.php?r=api/index/hits",{id:this.data.id}).then();
  },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if(options){
            this.data.id = options.id;
            this.get_content();
            this.hits();
        }
        this.get_rand_list();
        wx.setNavigationBarTitle({
            title: wx.getStorageSync('web_name'),
          })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        wx.hideLoading();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {
        return {
            title:  this.data.content.goods_name,
            imageUrl: this.data.content.thumb_pic,
            path: '/pages/content/index?id=' + this.data.id, 
          }
    },

    onShareTimeline(){
        return {
            title:  this.data.content.goods_name,
            imageUrl: this.data.content.thumb_pic,
            path: '/pages/content/index?id=' + this.data.id, 
          }
    },
})